package com.gonsin.venus.meeting.logic.controllers;

import com.gonsin.base.GPageRequest;
import com.gonsin.base.GPageResult;
import com.gonsin.base.ResponseResult;
import com.gonsin.base.exceptions.MyException;
import com.gonsin.venus.base.logic.auth.DeviceApi;
import com.gonsin.venus.base.logic.auth.WebApi;
import com.gonsin.venus.base.logic.keyOnly.KeyOnlyUtils;
import com.gonsin.venus.base.logic.session.SessionKeys;
import com.gonsin.venus.meeting.bean.TopicBean;
import com.gonsin.venus.meeting.interfaces.ITopicService;
import com.gonsin.venus.meeting.po.TopicPo;
import com.gonsin.venus.meeting.po.TopicSearchPo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "议题相关接口")
@Slf4j
@RestController
public class TopicController {

    @Autowired
    private ITopicService topicService;

    @WebApi
    @DeviceApi
    @ApiOperation(value = "议题 - 查询多个")
    @PostMapping("/restapi/topics")
    public ResponseResult<List<TopicBean>> searchTopics(
            GPageRequest pageRequest,
            @Valid @ApiParam("议题搜索对象") TopicSearchPo searchPo) {
        GPageResult<TopicBean> topicList = topicService.searchTopic(searchPo, pageRequest);
        return ResponseResult.result(topicList.getData());
    }

    @DeviceApi
    @WebApi
    @ApiOperation("议题 - 主键查询")
    @PostMapping("/restapi/topics/{topicKey}")
    public ResponseResult<TopicBean> findTopic(
            @ApiParam("议题的主键") @PathVariable("topicKey") String topicKey
    ) {
        return ResponseResult.result(topicService.getTopic(topicKey));
    }

    @WebApi
    @DeviceApi
    @ApiOperation("议题 - 添加")
    @PutMapping("/restapi/topics")
    public ResponseResult<TopicBean> addTopic(
            @RequestBody @ApiParam(value = "议题对象") TopicPo topicPo,
            @ApiIgnore HttpSession session) {
        return ResponseResult.result(topicService.addTopic(topicPo));
    }

    @WebApi
    @DeviceApi
    @ApiOperation("议题 - 删除")
    @DeleteMapping("/restapi/topics/{topicKey}")
    public ResponseResult<Void> removeTopic(
            @ApiParam("议题topic的key") @PathVariable("topicKey") String topicKey) {
        topicService.deleteTopic(topicKey);
        return ResponseResult.simpleResult();
    }

    @WebApi
    @DeviceApi
    @ApiOperation("议题 - 批量删除")
    @DeleteMapping("/restapi/topics")
    public ResponseResult<Void> removeTopic(
            @ApiIgnore HttpSession session,
            @ApiParam("议题topic的key") @RequestBody TopicSearchPo searchPo) {
        topicService.batchDeleteTopic(searchPo);
        return ResponseResult.simpleResult();
    }

    @WebApi
    @DeviceApi
    @ApiOperation("议题 - 修改")
    @PutMapping("/restapi/topics/{topicKey}")
    public ResponseResult<TopicBean> editTopic(
            @ApiParam("议题topic的key") @PathVariable("topicKey") String topicKey,
            @Valid @RequestBody @ApiParam("议题对象") TopicPo topicPo) {
        return ResponseResult.result(topicService.updateTopic(topicKey, topicPo));
    }

}
